package com.yunkont.pigcontrol.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yunkont.pigcontrol.R;
import com.yunkont.pigcontrol.app.BaseApplicaiton;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by bomei on 2017/9/23.
 */

public class SetTimeActivity extends AppCompatActivity {

    @Bind(R.id.edTime)
    EditText edTime;
    @Bind(R.id.submit)
    Button submit;
    private String url;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settime);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        url = intent.getStringExtra("url");
    }

    @OnClick({R.id.edTime, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edTime:
                break;
            case R.id.submit:
                sure();
                break;
        }
    }

    private void sure() {
        String s = edTime.getText().toString();
        if (TextUtils.isEmpty(s)) {
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        int i = Integer.parseInt(s);
        if (i < 1 || i > 100) {
            Toast.makeText(this, "请输入1~100之间的整数", Toast.LENGTH_SHORT).show();
            return;
        }
        toSetTime(s);

    }

    private void toSetTime(String time) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("pid", BaseApplicaiton.getInstance().getCurrentPid());
            jsonObject.put("st",time);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.e("toSetTime", "toSetTime: "+ url+" "+jsonObject.toString());

        OkHttpUtils.put()
                .url(url)
                .requestBody(jsonObject.toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(SetTimeActivity.this, "设置失败：" + e.toString(), Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Toast.makeText(SetTimeActivity.this, "设置成功", Toast.LENGTH_LONG).show();
                        finish();
                    }
                });
    }
}
